<?php

namespace App\Admin\Controllers;

use App\Models\Notice;
use App\Models\Student;
use App\Models\attendance_record;
use App\Models\dictation_score_record;
use App\Models\jiaxiao_record;
use App\Models\Term;
use App\Models\zuoye_record;
use App\Models\test_score_record;

use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class StudentDetailController extends Controller
{

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index(Request $request)
    {
        return Admin::content(function (Content $content) use($request){

            $content->header(trans('admin.student'));
            $content->description(trans('admin.list'));
            switch($request['type']){
                case 1:
                    $content->body($this->tingxie($request['id'])->render());
                    break;
                case 2:
                    $content->body($this->ceyan($request['id'])->render());
                    break;
                case 3:
                    $content->body($this->kaoqin($request['id'])->render());
                    break;
                case 4:
                    $content->body($this->jiaxiao($request['id'])->render());
                    break;
                case 5:
                    $content->body($this->zuoye($request['id'])->render());
                    break;
                default:break;
            }
            Admin::script($this->script());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {

    }

    /**
     * Show interface.
     *
     * @param mixed   $id
     * @param Content $content
     *
     * @return Content
     */
    public function show($id, Content $content)
    {

    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {

    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {

    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {

    }


    protected function kaoqin($id){
        $grid = new Grid(new attendance_record());

        $grid->model()->where('student_id',$id);

        $grid->student_id('学生')->display(function () use ($id) {
            return Student::find($id)->en_name;
        });

/*        $grid->attendance_status('考勤状态')->display(function (){
            if($this->attendance_status == '1'){
                return '正常';
            }elseif($this->attendance_status == '2'){
                return '请假';
            }elseif($this->attendance_status == '3'){
                return '迟到';
            }elseif($this->attendance_status == '4'){
                return '缺勤';
            }
        });*/

        $grid->column('考勤状态')->display(function(){
            $html = '<select class="form-control" id="'.$this->id.'-status">';
            if($this->attendance_status == '1'){
                $html .= '<option value="1" selected="selected">正常</option>';
                $html .= '<option value="2">请假</option>';
                $html .= '<option value="3">迟到</option>';
                $html .= '<option value="4">缺勤</option>';
                $html .= '<option value="5">家长代课</option>';
            }elseif($this->attendance_status == '2'){
                $html .= '<option value="1">正常</option>';
                $html .= '<option value="2" selected="selected">请假</option>';
                $html .= '<option value="3">迟到</option>';
                $html .= '<option value="4">缺勤</option>';
                $html .= '<option value="5">家长代课</option>';
            }elseif($this->attendance_status == '3'){
                $html .= '<option value="1">正常</option>';
                $html .= '<option value="2">请假</option>';
                $html .= '<option value="3" selected="selected">迟到</option>';
                $html .= '<option value="4">缺勤</option>';
                $html .= '<option value="5">家长代课</option>';
            }elseif($this->attendance_status == '4'){
                $html .= '<option value="1">正常</option>';
                $html .= '<option value="2">请假</option>';
                $html .= '<option value="3">迟到</option>';
                $html .= '<option value="4" selected="selected">缺勤</option>';
                $html .= '<option value="5">家长代课</option>';
            }elseif($this->attendance_status == '5'){
                $html .= '<option value="1">正常</option>';
                $html .= '<option value="2">请假</option>';
                $html .= '<option value="3">迟到</option>';
                $html .= '<option value="4">缺勤</option>';
                $html .= '<option value="5" selected="selected">家长代课</option>';
            }
            $html .= '</select>';
            return $html;
        });

        $grid->column('保存')->display(function(){
            $str = "'kaoqin'";
            $html = '<button class="btn btn-primary" onclick="save('.$this->id.','.$str.')">保存修改</button>';
            return $html;
        });

        $grid->column('考勤时间')->display(function(){
            return $start_date = Notice::find($this->notice_id)->starttime;
        });

        $grid->column('周')->display(function(){
            $start_date = Term::where('status','=','0')->get(['start_date'])->toArray();
            return ceil((strtotime(Notice::find($this->notice_id)->starttime) - strtotime($start_date[0]['start_date']))/3600/24/7);
        });

        $grid->column('事件')->display(function(){
            return $start_date = Notice::find($this->notice_id)->event;
        });

        //禁用导出按钮
        $grid->disableExport();
        // $squad_name = Squad::find($id)->name();
        // $filename=$squad_name."学生列表";
        // $grid->exporter(new ExcelExpoter($grid,$filename));

        //禁用创建按钮
        $grid->disableCreateButton();
        $grid->disableRowSelector();
        $grid->disableActions();

/*        $grid->actions(function ($actions) {
            $actions->disableDelete();
            $actions->disableEdit();
            $actions->disableView();
        });*/
/*        $grid->tools(function ($tools) {
            //关闭批量删除
            $tools->batch(function ($batch) {
                $batch->disableDelete();
            });
        });*/


        $grid->filter(function (Grid\Filter $filter) {

            //$filter->expand();

            $filter->column(1/2, function ($filter) {
                $filter->disableIdFilter();
                $filter->equal('attendance_status', '考勤状态')->select($options = ['1'=>'正常','2'=>'请假','3'=>'迟到','4'=>'缺勤']);
            });
            $filter->column(1/2, function ($filter) {
                $filter->between('create_at', '考勤时间')->datetime();
            });
        });
        return $grid;
    }

    protected function tingxie($id){
        $grid = new Grid(new dictation_score_record());

        $grid->model()->where('student_id',$id);

        $grid->student_id('学生')->display(function () use ($id) {
            return Student::find($id)->en_name;
        });

/**/
        //$grid->dictation_status('听写状态')->editable('select',['1'=>'正常','2'=>'未参加']);
        $grid->column('听写状态')->display(function(){
            $html = '<select class="form-control" id="'.$this->id.'-status">';
            if($this->dictation_status == '1'){
                $html .= '<option value="1" selected="selected">正常</option>';
                $html .= '<option value="2">未参加</option>';
            }elseif($this->dictation_status == '2'){
                $html .= '<option value="1">正常</option>';
                $html .= '<option value="2" selected="selected">未参加</option>';
            }
            $html .= '</select>';
            return $html;
        });

        //$grid->dictation_score('成绩');
        $grid->column('成绩')->display(function(){
            $html = '<input type="text" id="'.$this->id.'-score" value="'.$this->dictation_score.'"/>';
            $str = "'dictation'";
            $html .= '<button class="btn btn-primary" style="margin-left:20px" onclick="save('.$this->id.','.$str.')">保存修改</button>';
            return $html;
        });

        $grid->column('听写时间')->display(function(){
            return $start_date = Notice::find($this->notice_id)->starttime;
        });

        $grid->column('周')->display(function(){
            $start_date = Term::where('status','=','0')->get(['start_date'])->toArray();
            return ceil((strtotime(Notice::find($this->notice_id)->starttime) - strtotime($start_date[0]['start_date']))/3600/24/7);
        });

        $grid->column('事件')->display(function(){
            return $start_date = Notice::find($this->notice_id)->event;
        });

        $grid->dictation_desc('备注');

        //禁用导出按钮
        $grid->disableExport();
        // $squad_name = Squad::find($id)->name();
        // $filename=$squad_name."学生列表";
        // $grid->exporter(new ExcelExpoter($grid,$filename));

        //禁用创建按钮
        $grid->disableCreateButton();
        $grid->disableRowSelector();
        $grid->disableActions();

/*        $grid->actions(function ($actions) {
            $actions->disableDelete();
            $actions->disableEdit();
            $actions->disableView();
            $str = "dictation";
            $actions->append('<a class="glyphicon glyphicon-pencil" onclick="save('.$actions->getKey().','.$str.')"></a>');
        });*/

/*        $grid->tools(function ($tools) {
            //关闭批量删除
            $tools->batch(function ($batch) {
                $batch->disableDelete();
            });
        });*/


        $grid->filter(function (Grid\Filter $filter) {

            //$filter->expand();

            $filter->column(1/2, function ($filter) {
                $filter->disableIdFilter();
                $filter->equal('dictation_status', '听写状态')->select($options = ['1'=>'正常','2'=>'未参加']);
            });
            $filter->column(1/2, function ($filter) {
                $filter->between('create_at', '听写时间')->datetime();
            });
        });
        return $grid;
    }

    protected function ceyan($id){
        $grid = new Grid(new test_score_record());

        $grid->model()->where('student_id',$id);

        $grid->student_id('学生')->display(function () use ($id) {
            return Student::find($id)->en_name;
        });

        $grid->column('测试状态')->display(function(){
            $html = '<select class="form-control" id="'.$this->id.'-status">';
            if($this->test_status == '1'){
                $html .= '<option value="1" selected="selected">正常</option>';
                $html .= '<option value="2">未参加</option>';
            }elseif($this->test_status == '2'){
                $html .= '<option value="1">正常</option>';
                $html .= '<option value="2" selected="selected">未参加</option>';
            }
            $html .= '</select>';
            return $html;
        });

        //$grid->test_score('成绩');
        $grid->column('成绩')->display(function(){
            $html = '<input type="text" id="'.$this->id.'-score" value="'.$this->test_score.'"/>';
            $str = "'test'";
            $html .= '<button class="btn btn-primary" style="margin-left:20px" onclick="save('.$this->id.','.$str.')">保存修改</button>';
            return $html;
        });

        $grid->column('测试时间')->display(function(){
            return $start_date = Notice::find($this->notice_id)->starttime;
        });

        $grid->column('周')->display(function(){
            $start_date = Term::where('status','=','0')->get(['start_date'])->toArray();
            return ceil((strtotime(Notice::find($this->notice_id)->starttime) - strtotime($start_date[0]['start_date']))/3600/24/7);
        });

        $grid->column('事件')->display(function(){
            return $start_date = Notice::find($this->notice_id)->event;
        });

        $grid->test_desc('备注');

        //禁用导出按钮
        $grid->disableExport();
        // $squad_name = Squad::find($id)->name();
        // $filename=$squad_name."学生列表";
        // $grid->exporter(new ExcelExpoter($grid,$filename));

        //禁用创建按钮
        $grid->disableCreateButton();
        $grid->disableRowSelector();
        $grid->disableActions();

        /*        $grid->actions(function ($actions) {
                    $actions->disableDelete();
                    $actions->disableEdit();
                    $actions->disableView();
                });*/
        /*        $grid->tools(function ($tools) {
                    //关闭批量删除
                    $tools->batch(function ($batch) {
                        $batch->disableDelete();
                    });
                });*/


        $grid->filter(function (Grid\Filter $filter) {

            //$filter->expand();

            $filter->column(1/2, function ($filter) {
                $filter->disableIdFilter();
            });
            $filter->column(1/2, function ($filter) {
                $filter->between('create_at', '测试时间')->datetime();
            });
        });
        return $grid;
    }

    protected function jiaxiao($id){
        $grid = new Grid(new jiaxiao_record());

        $grid->model()->where('student_id',$id);

        $grid->student_id('学生')->display(function () use ($id) {
            return Student::find($id)->en_name;
        });

/*        $grid->jiaxiao_status('家校状态')->display(function (){
            if($this->jiaxiao_status == '1'){
                return '准交';
            }elseif($this->jiaxiao_status == '2'){
                return '未交';
            }elseif($this->jiaxiao_status == '3'){
                return '补交';
            }
        });*/

        $grid->column('家校状态')->display(function(){
            $html = '<select class="form-control" id="'.$this->id.'-status">';
            if($this->jiaxiao_status == '1'){
                $html .= '<option value="1" selected="selected">准交</option>';
                $html .= '<option value="2">未交</option>';
                $html .= '<option value="3">补交</option>';
            }elseif($this->jiaxiao_status == '2'){
                $html .= '<option value="1">准交</option>';
                $html .= '<option value="2" selected="selected">未交</option>';
                $html .= '<option value="3">补交</option>';
            }elseif($this->jiaxiao_status == '3'){
                $html .= '<option value="1">准交</option>';
                $html .= '<option value="2">未交</option>';
                $html .= '<option value="3" selected="selected">补交</option>';
            }
            $html .= '</select>';
            return $html;
        });

        //$grid->jiaxiao_score('成绩');

        $grid->column('成绩')->display(function(){
            $html = '<input type="text" id="'.$this->id.'-score" value="'.$this->jiaxiao_score.'"/>';
            $str = "'jiaxiao'";
            $html .= '<button class="btn btn-primary" style="margin-left:20px" onclick="save('.$this->id.','.$str.')">保存修改</button>';
            return $html;
        });

        $grid->column('家校时间')->display(function(){
            return $start_date = Notice::find($this->notice_id)->starttime;
        });

        $grid->column('周')->display(function(){
            $start_date = Term::where('status','=','0')->get(['start_date'])->toArray();
            return ceil((strtotime(Notice::find($this->notice_id)->starttime) - strtotime($start_date[0]['start_date']))/3600/24/7);
        });

        $grid->column('事件')->display(function(){
            return $start_date = Notice::find($this->notice_id)->event;
        });

        $grid->jiaxiao_desc('备注');

        //禁用导出按钮
        $grid->disableExport();
        // $squad_name = Squad::find($id)->name();
        // $filename=$squad_name."学生列表";
        // $grid->exporter(new ExcelExpoter($grid,$filename));

        //禁用创建按钮
        $grid->disableCreateButton();
        $grid->disableRowSelector();
        $grid->disableActions();

        /*        $grid->actions(function ($actions) {
                    $actions->disableDelete();
                    $actions->disableEdit();
                    $actions->disableView();
                });*/
        /*        $grid->tools(function ($tools) {
                    //关闭批量删除
                    $tools->batch(function ($batch) {
                        $batch->disableDelete();
                    });
                });*/


        $grid->filter(function (Grid\Filter $filter) {

            //$filter->expand();

            $filter->column(1/2, function ($filter) {
                $filter->disableIdFilter();
                $filter->equal('jiaxiao_status', '听写状态')->select($options = ['1'=>'准交','2'=>'未交','3'=>'补交']);
            });
            $filter->column(1/2, function ($filter) {
                $filter->between('create_at', '家校时间')->datetime();
            });
        });
        return $grid;
    }

    protected function zuoye($id){
        $grid = new Grid(new zuoye_record());

        $grid->model()->where('student_id',$id);

        $grid->student_id('学生')->display(function () use ($id) {
            return Student::find($id)->en_name;
        });

        $grid->column('作业状态')->display(function(){
            $html = '<select class="form-control" id="'.$this->id.'-status">';
            if($this->zuoye_status == '1'){
                $html .= '<option value="1" selected="selected">准交</option>';
                $html .= '<option value="2">未交</option>';
                $html .= '<option value="3">补交</option>';
            }elseif($this->zuoye_status == '2'){
                $html .= '<option value="1">准交</option>';
                $html .= '<option value="2" selected="selected">未交</option>';
                $html .= '<option value="3">补交</option>';
            }elseif($this->zuoye_status == '3'){
                $html .= '<option value="1">准交</option>';
                $html .= '<option value="2">未交</option>';
                $html .= '<option value="3" selected="selected">补交</option>';
            }
            $html .= '</select>';
            return $html;
        });

        //$grid->zuoye_score('成绩');

        $grid->column('成绩')->display(function(){
            $html = '<input type="text" id="'.$this->id.'-score" value="'.$this->zuoye_score.'"/>';
            $str = "'zuoye'";
            $html .= '<button class="btn btn-primary" style="margin-left:20px" onclick="save('.$this->id.','.$str.')">保存修改</button>';
            return $html;
        });

        $grid->column('作业时间')->display(function(){
            return $start_date = Notice::find($this->notice_id)->starttime;
        });

        $grid->column('周')->display(function(){
            $start_date = Term::where('status','=','0')->get(['start_date'])->toArray();
            return ceil((strtotime(Notice::find($this->notice_id)->starttime) - strtotime($start_date[0]['start_date']))/3600/24/7);
        });

        $grid->column('事件')->display(function(){
            return $start_date = Notice::find($this->notice_id)->event;
        });

        $grid->zuoye_desc('备注');

        //禁用导出按钮
        $grid->disableExport();
        // $squad_name = Squad::find($id)->name();
        // $filename=$squad_name."学生列表";
        // $grid->exporter(new ExcelExpoter($grid,$filename));

        //禁用创建按钮
        $grid->disableCreateButton();
        $grid->disableRowSelector();
        $grid->disableActions();

        /*        $grid->actions(function ($actions) {
                    $actions->disableDelete();
                    $actions->disableEdit();
                    $actions->disableView();
                });*/
        /*        $grid->tools(function ($tools) {
                    //关闭批量删除
                    $tools->batch(function ($batch) {
                        $batch->disableDelete();
                    });
                });*/


        $grid->filter(function (Grid\Filter $filter) {

            //$filter->expand();

            $filter->column(1/2, function ($filter) {
                $filter->disableIdFilter();
                $filter->equal('zuoye_status', '作业状态')->select($options = ['1'=>'准交','2'=>'未交','3'=>'补交']);
            });
            $filter->column(1/2, function ($filter) {
                $filter->between('create_at', '作业时间')->datetime();
            });
        });
        return $grid;
    }

    public function script()
    {
        return <<<SCRIPT
        
        save=function(id,type){
            var request={type:type};
            request.id = id;
            if(type == 'dictation' || type == 'test' || type == 'zuoye' || type=='jiaxiao'){
                request.status = $('#'+id+'-status').val();
                request.score = $('#'+id+'-score').val();
            }else{
                request.status = $('#'+id+'-status').val();
            }
            
            //$.ajaxSetup({headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')},async:false});
/*            $.post('/admin/student_detail/save',request,function(e){
                alert(e.message);
            });*/
                                                    $.ajax({
                                method: 'post',
                                url: '/admin/student_detail/save',
                                data: {
                                    _token:LA.token,
                                    request:request
                                },
                                success: function () {
                                    toastr.success('修改成功');
                                    parent.location.reload();
                                }
                            });
        }

SCRIPT;
    }

    public function save(Request $request)
    {
        if($request['request']['type'] == 'dictation'){
            DB::table('dictation_score_records')->where('id',$request['request']['id'])->update(['dictation_status'=>$request['request']['status'],'dictation_score'=>$request['request']['score']]);
        }elseif($request['request']['type'] == 'test'){
            DB::table('test_score_records')->where('id',$request['request']['id'])->update(['test_status'=>$request['request']['status'],'test_score'=>$request['request']['score']]);
        }elseif($request['request']['type'] == 'jiaxiao'){
            DB::table('jiaxiao_records')->where('id',$request['request']['id'])->update(['jiaxiao_status'=>$request['request']['status'],'jiaxiao_score'=>$request['request']['score']]);
        }elseif($request['request']['type'] == 'zuoye'){
            DB::table('zuoye_records')->where('id',$request['request']['id'])->update(['zuoye_status'=>$request['request']['status'],'zuoye_score'=>$request['request']['score']]);
        }elseif($request['request']['type'] == 'kaoqin'){
            DB::table('attendance_records')->where('id',$request['request']['id'])->update(['attendance_status'=>$request['request']['status']]);
        }
        return array('message'=>'操作成功!');
    }
}
